package com.shun.upms.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shun.common.R;
import com.shun.common.utils.ShunCommonUtils;
import com.shun.upms.common.UpmsConstant;
import com.shun.upms.dto.SysAdminDeptDto;
import com.shun.upms.mapper.SysAdminDeptMapper;
import com.shun.upms.mapper.SysAdminUserMapper;
import com.shun.upms.service.ISysAdminDeptService;
import com.shun.upms.vo.SysAdminDept;
import com.shun.upms.vo.SysAdminUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 系统内部机构表 服务实现类
 * </p>
 *
 * @author clever
 * @since 2021-10-03
 */
@Slf4j
@Service
public class SysAdminDeptServiceImpl extends ServiceImpl<SysAdminDeptMapper, SysAdminDept> implements ISysAdminDeptService {

    @Autowired
    private SysAdminDeptMapper deptMapper;

    @Autowired
    private SysAdminUserMapper userMapper;

    @Override
    public R getDeptNameAndId() {
        List<Map<String, Object>> nameAndIds = list().stream().map(d -> {
            Map<String, Object> nameAndIdMap = new HashMap<String, Object>();
            nameAndIdMap.put("id", d.getDeptId());
            nameAndIdMap.put("name", d.getDeptSimpleName());
            return nameAndIdMap;
        }).collect(Collectors.toList());
        return R.suc(nameAndIds);
    }

    /**
     * 获取部门树
     * @return
     */
    public R getTreeDepts(){
        return R.suc(deptMapper.getDeptNodes("0"));
    }

    @Override
    public R addDept(SysAdminDeptDto dto) {
        SysAdminDept dept = ShunCommonUtils.beanMap(dto,SysAdminDept.class);
        String deptId = IdUtil.objectId();
        dept.setDeptId(deptId);
        if(ObjectUtil.isEmpty(dto.getDeptPid())){
            dept.setDeptPid(UpmsConstant.DEPT_ROOT_ID);
        }
        return save(dept)?R.suc():R.error(UpmsConstant.ADD_ADMIN_DEPT_ERROR);
    }

    @Override
    public R editDept(SysAdminDeptDto dto) {
        SysAdminDept sysAdminDept = ShunCommonUtils.beanMap(dto,SysAdminDept.class);
        UpdateWrapper<SysAdminDept> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("dept_id",dto.getDeptId());
        return update(sysAdminDept,updateWrapper)?R.suc():R.error("");
    }

    @Override
    public R checkDeptSimpleName(String simpleName) {
        QueryWrapper<SysAdminDept> deptQueryWrapper = new QueryWrapper<>();
        deptQueryWrapper.eq("dept_simple_name",simpleName);
        List<SysAdminDept> deptList = list(deptQueryWrapper);
        return (ObjectUtil.isEmpty(deptList))?R.suc():R.error("已经存在！");
    }

    @Override
    public R delDept(String deptId) {
        log.debug("deptId:{}",deptId);
        List<SysAdminUser> sysAdminUsers = new LambdaQueryChainWrapper<SysAdminUser>(this.userMapper)
                .eq(SysAdminUser::getSauDeptId,deptId).list();
        if(ObjectUtil.isEmpty(sysAdminUsers)){
              return removeById(deptId)?R.suc():R.error("删除部门失败");
        }else{
            log.debug("部门员工数：{}",sysAdminUsers.size());
            return R.error("当前部门下有用户，不能进行删除！");
        }
    }
}
